{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Main where

import GHCJS.DOM
import GHCJS.DOM.Document
import GHCJS.DOM.Element (setAttribute)
import GHCJS.DOM.Node
import GHCJS.DOM.WebGLRenderingContextBase
import GHCJS.DOM.HTMLCanvasElement (getContext)
import GHCJS.DOM.Types hiding (Text)
import Data.FileEmbed (embedFile)
import Data.Text.Encoding (decodeUtf8)

import Data.Coerce (coerce)
main :: IO ()
main = runWebGUI $ \win -> do
  enableInspector win
  Just doc <- webViewGetDomDocument win
  Just body <- getBody doc
  Just canvas <- createElement doc (Just "canvas")
  setAttribute canvas "width" "400"
  setAttribute canvas "height" "400"
  _ <- appendChild body (Just canvas)

  gl :: WebGLRenderingContextBase <- coerce <$> getContext (coerce canvas) "webgl"
  let vertShaderSource = decodeUtf8 $(embedFile "shader/graph.vert")
      fragShaderSource = decodeUtf8 $(embedFile "shader/graph.frag")

  frg <- createShader gl FRAGMENT_SHADER
  shaderSource gl frg fragShaderSource
  compileShader gl frg

  vert <- createShader gl VERTEX_SHADER
  shaderSource gl vert vertShaderSource
  compileShader gl vert

  prog <- createProgram gl
  attachShader gl prog frg
  attachShader gl prog vert
  linkProgram gl prog
  useProgram gl prog

  clearColor gl 0.0 0.0 0.0 1.0
  clear gl COLOR_BUFFER_BIT
  drawArrays gl POINTS 0 1
